<template>
  <div class="admin">
    <el-bread></el-bread>

    <el-button @click="showAdd" class="el-btn" type="primary" size="small" plain
      >添加</el-button
    >

    <el-table
      :data="userList"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="uid" label="用户编号" width="300">
      </el-table-column>
      <el-table-column prop="username" label="角色名称" width="180">
      </el-table-column>
      <el-table-column prop="rolename" label="所属角色" width="180">
      </el-table-column>
      <el-table-column label="状态">
        <template v-slot="a">
          <el-tag v-if="a.row.status == 1" type="success">启用</el-tag>
          <el-tag v-else type="warning">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="a">
          <el-button size="small" type="primary" @click="infoShow(a.row.uid)"
            >编辑</el-button
          >
          <el-button
            size="small"
            v-if="a.row.uid != '7f412140-6b72-11ea-a476-bbdc6fb709e3'"
            type="danger"
            @click="del(a.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      @current-change="changePage"
      background
      layout="prev, pager, next"
      :total="userCount"
      :page-size="pageData.size"
      :current-page="pageData.page"
    >
    </el-pagination>

    <v-dialog :pageData="pageData" ref="dialog" />
  </div>
</template>

<script>
import { userdelete } from "../../request/api";
import { mapState, mapActions } from "vuex";
import vDialog from "./admin-dialog.vue";
export default {
  data() {
    return {};
  },
  components: {
    vDialog,
  },
  mounted() {
    // this.getUserList();
    this.getUserCount();
  },
  computed: {
    ...mapState("admin", ["userList", "userCount", "pageData"]),
  },
  methods: {
    ...mapActions("admin", ["getUserList", "getUserCount"]),
    del(id) {
      this.$del(() => {
        userdelete({ id }).then((res) => {
          if (res.code == 200) {
            this.$message.success(res.msg);
            // this.getUserList();
            this.getUserCount();
            return;
          }
          this.$message.error(res.msg);
        });
      });
    },
    // 显示新增页面
    showAdd() {
      this.$refs.dialog.isshow = true;
    },
    // 显示编辑页面
    infoShow(uid) {
      this.$refs.dialog.getInfo(uid);
    },
    // 当页码发生改变请求对应页面的数据
    changePage(e) {
      this.$store.commit("admin/changePageData", e);
      this.getUserList();
    },
  },
};
</script>

<style scoped>
.el-btn {
  display: block;
  margin: 20px 0 5px 0;
}
</style>